/**
 * 
 */
package session;

import java.io.Serializable;

import appserver.Constants;


/**
 * @author Jose
 *
 */
@SuppressWarnings("serial")
public class SessionID implements Serializable {
	private String sessionID;
	private static final int APP_SERVER_ID_LENGTH = 15;
	private static final int LOCAL_SESSION_ID_LENGTH = 6;

	public SessionID(String appServerID, int localSessionID) {
		this.sessionID = generateSessionID(appServerID, localSessionID);
	}
	
	public SessionID(String sessionID) {
		this.sessionID = sessionID;
	}
	
	public String getSessionID() {
		return sessionID;
	}

	public void setSessionID(String sessionID) {
		this.sessionID = sessionID;
	}
	
	private static String generateSessionID(String appServerID, int localSessionID) {
			//String appServer = String.format("%0" + APP_SERVER_ID_LENGTH + "d", appServerID);
			//String localSession = String.format("%0" + LOCAL_SESSION_ID_LENGTH + "d", localSessionID);
		
			String id = localSessionID + Constants.SESSION_ID_SEPARATOR + appServerID;
			return id;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		
		if (!(obj instanceof SessionID)) return false;
		
		SessionID sessionID2 = (SessionID) obj;
		
		if (sessionID2.getSessionID() == null) return false;
		
		if (sessionID.equals(sessionID2.getSessionID())) return true;
		
		return false;
	}
	
	@Override
	  public int hashCode() { 
	    int hash = 1;
	    hash = hash * 31 + sessionID.hashCode();
	    return hash;
	  }
	
	@Override
	public String toString() {
		return ("" + this.sessionID);
	}
	
	public static SessionID parseString(String sessIdString) {
		SessionID sessId = new SessionID(sessIdString);
		return sessId;
	}

}
